package debtbundler.presentation;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import debtbundler.domain.entity.BalanceSheet;
import debtbundler.presentation.utils.IconConstants;
import debtbundler.presentation.utils.UIUtils;

/**
 * Renders <code>BalanceSheet</code> objects as list cells, displaying 
 * sheet name and ahead of the name a a lock symbol ahead if the sheet is signed.
 * 
 * @author Anssi
 *
 */
public class BalanceSheetListCellRenderer extends JLabel implements
		ListCellRenderer {
	
	private static final long serialVersionUID = 1L;
	private static final Icon SIGNED_SHEET_ICON = UIUtils.getImageIcon(IconConstants.SIGNED_BALANCE_SHEET_16);
	private static final Icon UNSIGNED_SHEET_ICON = UIUtils.getImageIcon(IconConstants.BALANCE_SHEET_16);

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		
		BalanceSheet sheet = (BalanceSheet) value;
		setText(sheet.getName());
		if (sheet.getSignature() == null) {
			setIcon(UNSIGNED_SHEET_ICON);
		} else {
			setIcon(SIGNED_SHEET_ICON);
		}
		
		if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        
        return this;
	}

}
